package com.example.websocketstompdemo.controller;


import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
public class WebSocketController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @MessageMapping(value = "msgq")
    @SendTo(value = "/topic/mymsg")
    public String testsocket(String msg) {
        log.info(msg);
        simpMessagingTemplate.convertAndSend("/topic/callback", "推送: "+msg);
        return msg;
    }

    @MessageMapping(value = "info")
    public void sdfsdf(@RequestParam String t) {
        System.out.println(t);
    }

    @MessageMapping(value = "touser")
    public void touser(String val) {
        JSONObject json = JSONObject.parseObject(val);
        simpMessagingTemplate.convertAndSendToUser(json.getString("usertoken"), "/topic/mymsg", json.getString("msg"));
    }

}
